<?php
require 'auth.php';
require '../config/config.php';

if(!isset($_GET['id'])){
	header("Location: ../posts.php");
	exit();
}
mydb_connect();

$qry = $conn->prepare("SELECT * FROM `news` WHERE id = :id");
$qry->execute(array('id' => $_GET['id']));
if(!$qry) echo die($qry->errorInfo());

$row = $qry->fetch();
if(file_exists("../../author/images/" . $row['img_name'])){
	$unlink = unlink("../../author/images/" . $row['img_name']);
	if($unlink){
		$delete = $conn->prepare("DELETE FROM `news` WHERE id = :id");
		$delete->execute(array('id' => $_GET['id']));

		if($delete){
			header("Location:../posts.php?delete=success");
			exit();
		}
		else{
			echo $delete->errorInfo();
		}
	}
} else {
	$delete = $conn->prepare("DELETE FROM `news` WHERE id = :id");
		$delete->execute(array('id' => $_GET['id']));
	if($delete){
			header("Location:../posts.php?delete=success");
			exit();
		}
		else{
			echo $delete->errorInfo();
		}

}
?>